function relod() {
    layui.use('table', function() {
        var table = layui.table;
        table.reload('test', {
            page: {
                curr: 1 //重新从第一页开始
            }, where: {
                startTime: $("#test1").val(),
                endTime: $("#test2").val(),
                pid: $("#cgd").val(),
                status: 3,
                payStatus: 2
            }
        })
    })
}
layui.use(['table','form','layer', 'layedit', 'laydate'], function(){
    var table = layui.table,laydate = layui.laydate,layedit = layui.layedit
    var form = layui.form
    form.render("select");
    //日期
    laydate.render({
        elem: '#date'
    });
    laydate.render({
        elem: '#date1'
    });
    layui.$("#look").on('click',function () {
        if ($("#test1").val()!=''&&$("#test2").val()==''){
            relod();
        } else {
            if($("#test1").val()>$("#test2").val()){
                layer.msg("结束时间不能小于开始时间")
            }else {
                relod();
            }
        }
    })
    table.render({
        elem: '#test'
        ,url:'htz/purchaseOrder/all',
        parseData: function(res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.obj.total, //解析数据长度
                "data": res.obj.list //解析数据列表
            };
        }
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            ,layEvent: 'LAYTABLE_TIPS'
            ,icon: 'layui-icon-tips'
        }]
        ,title: '用户数据表'
        ,cols: [[
            {type: 'radio', fixed: 'left',width:40}
            ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
            ,{field:'purchaseOrderCode', title:'采购单Id', width:120,unresize: true, sort: true}
            ,{field:'sourceId', title:'上游单号', width:120, templet: function(res){
                    if (res.sourceId==null){
                        return "无"
                    }
                    return res.sourceId
                }}
            ,{field:'createDate', title:'生成采购单时间', width:180}
            ,{field:'purchaseOrderCost', title:'预计总金额', width:120}
            ,{field:'status', title:'采购单状态', width:120,templet:function (res) {
                    switch (res.status) {
                        case 1:
                            return "新录入";
                        case 2:
                            return "待审核";
                        case 3:
                            return "审核通过";
                        case 4:
                            return "审核不通过";
                        case 5:
                            return "采购单进行中";
                        case 6:
                            return "已完成";
                    }
                }}
            ,{field:'purchaserId', title:'采购员Id', width:120}
            ,{field:'auditorId', title:'审核人Id', width:120,templet:function (res) {
                    if (res.auditorId==''||res.auditorId==null){
                        return "无"
                    }
                    return res.auditorId;
                }}
            ,{field:'type', title:'采购单类型', width:120,templet:function (data) {
                    switch (data.type) {
                        case 1:
                            return "新品采购";
                        case 2:
                            return "补货";
                    }
                }}
            ,{field:'payStatus', title:'采购单类型', width:120,templet:function (data) {
                    switch (data.payStatus) {
                        case 1:
                            return "未付款";
                        case 2:
                            return "已付款";
                    }
                }}
            // ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:180}
        ]],
        page: true
        ,where:{
            payStatus: 2,
            status: 3
        }
    });
    //监听头部工具行
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'insertPurchase' :
                layer.open({
                    type: 2,
                    area: ['750px','380px'],
                    content: 'purchase_add.html' //这里content是一个普通的String
                });
                break;
            case 'auditPurchase':
                var data = checkStatus.data;
                if (data==''){
                    layer.msg("请选择一个采购单")
                }else {
                    parent.getPurchase(data);
                    parent.layer.closeAll();
                    // layer.confirm('确认变为可审核',function (index) {
                    //     var flag = false;
                    //     console.log(data);
                    //     for (var i = 0; i <data.length ; i++) {
                    //         if (data[i].status>1){
                    //             layer.msg("只有新录入的才能变为待审核")
                    //             flag = true;
                    //             break;
                    //         }
                    //     }
                    //     if (!flag){
                    //         $.ajax({
                    //             type: "post",
                    //             url: "htz/purchaseOrder/updAuditBylist",
                    //             data: JSON.stringify(data),
                    //             contentType: "application/json;charset=UTF-8",
                    //             dataType: "json",
                    //             success:function (data) {
                    //                 if (data.obj>0){
                    //                     layer.msg("修改成功");
                    //                     relod();
                    //                 }else {
                    //                     layer.msg("修改失败")
                    //                 }
                    //             }
                    //         })
                    //     }
                    // });
                }
                break;
        }
    });
    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layer = layui.layer;
        //console.log(obj)
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                if(data.status>1){
                    layer.msg("只能删除新录入的采购单");
                }else {
                    $.post("htz/purchaseOrder/del",{id:data.id},function (res) {
                        if (res.obj>0){
                            obj.del();
                            layer.close(index);
                            layer.msg('删除成功');
                        }else {
                            layer.msg("删除失败")
                        }
                    })
                }
            });
        } else if(obj.event === 'edit') {
            layer.open({
                type: 2,
                area: ['600px','310px'],
                content: 'purchase_detail1.html' //这里content是一个普通的String
            });
        } else if(obj.event === 'view'){
            layer.open({
                type: 2,
                area: ['600px','310px'],
                content: 'purchase_detail.html', //这里content是一个普通的String
                success:function (layero,index) {
                    var iframe = window['layui-layer-iframe'+index]
                    iframe.tes(data);
                }
            });
        }
    });

});
layui.use('laydate',function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#test1',
        trigger: 'click'
    });
    laydate.render({
        elem: '#test2',
        trigger: 'click'
    });
})